package com.elec.service.impl;

import cn.hutool.captcha.generator.RandomGenerator;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elec.domain.LoginUser;
import com.elec.domain.R;
import com.elec.domain.entity.User;
import com.elec.mapper.UserMapper;
import com.elec.service.UserService;
import com.elec.util.BeanCopyUtils;
import com.elec.util.JwtUtil;
import com.elec.util.ThreadLocalUtil;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 存储用户基本信息(User)表服务实现类
 *
 * @author yu
 * @since 2024-07-05 15:11:02
 */
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public R add(User user) {
        if (user.getUsername() == null || user.getPassword() == null) {
            return R.error("用户名或密码不能为空");
        }
        if (this.save(user)) {
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }

    @Override
    public R delete(Long id) {
        if (this.removeById(id)) {
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

    @Override
    public R update(User user) {
        if (this.updateById(user)) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }

    @Override
    public R get(Long id) {
        if (this.getById(id) != null) {
            return R.success(this.getById(id));
        }
        return R.error("查询失败");
    }

    @Override
    public R getList(Integer pageNum, Integer pageSize, User user) {
        if (pageNum == null || pageSize == null) {
            return R.error("页码或页数不能为空");
        }
        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
        if (user != null && user.getUsername() != null && !user.getUsername().equals("")){
            userWrapper.like(User::getUsername, user.getUsername());
        }
        Page<User> page = new Page<>(pageNum, pageSize);
        page(page, userWrapper);
        return R.success(page.getRecords(), page.getTotal());
    }

    @Override
    public R getMyInfo() {
        Long userId = ThreadLocalUtil.getLocal();
        if (userId != null) {
            User user = this.getById(userId);
            return R.success(user);
        }
        return null;
    }

    @Override
    public R updateMyInfo(User user) {
        Long userId = ThreadLocalUtil.getLocal();
        user.setUserId(userId.intValue());
        boolean b = updateById(user);
        if (b){
            return R.success("信息修改成功");
        }
        return R.error("修改失败");
    }

    @Override
    public R login(User user) {
        if (Objects.isNull(user)){
            return R.error("请输入用户名和密码");
        }
        if (Objects.isNull(user.getUsername()) || Objects.isNull(user.getPassword()) || "".equals(user.getUsername()) || "".equals(user.getPassword())){
            return R.error("请输入用户名或密码");
        }
        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.eq(User::getUsername, user.getUsername());
        userWrapper.eq(User::getPassword, SecureUtil.md5(user.getPassword()));
        User one = getOne(userWrapper);
        if (Objects.isNull(one)){
            return R.error("用户名或密码错误");
        }
        //登录成功
        String userId = one.getUserId()+"";
        String token = JwtUtil.createJWT(userId);
        LoginUser loginUser = BeanCopyUtils.copyBean(one, LoginUser.class);
        loginUser.setToken(token);
        return R.success(loginUser);
    }

    @Override
    public R register(User user) {
        if (Objects.isNull(user)){
            return R.error("请输入用户名和密码");
        }
        if (Objects.isNull(user.getUsername()) || Objects.isNull(user.getPassword()) || "".equals(user.getUsername()) || "".equals(user.getPassword())){
            return R.error("请输入用户名或密码");
        }
        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.eq(User::getUsername, user.getUsername());
        User one = getOne(userWrapper);
        if (!Objects.isNull(one)){
            return R.error("用户名重复，请重新输入用户名");
        }
        //自动生成昵称
        user.setPassword(SecureUtil.md5(user.getPassword()));
        boolean save = save(user);
        if (save){
            return R.success("注册成功");
        }
        return R.error("注册失败");
    }
}


